package com.helper.dream.a361phonehelper.fragment.detail;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.google.gson.Gson;
import com.helper.dream.a361phonehelper.R;
import com.helper.dream.a361phonehelper.adapter.VideoAdapter;
import com.helper.dream.a361phonehelper.bean.VideoBean;
import com.helper.dream.a361phonehelper.fragment.RedianFragment;
import com.helper.dream.a361phonehelper.utils.ApiManager;
import com.helper.dream.a361phonehelper.utils.DownString;
import com.helper.dream.a361phonehelper.utils.L;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;



/**
 * Created by Administrator on 2016/12/21.
 */

public class VideoFragment extends Fragment {
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            adapter.notifyDataSetChanged();
//            L.d("videoUrl:" + list.get(0).aboutRadioUrl);
//            L.d("handle");
        }
    };
    private VideoAdapter adapter;
    private RecyclerView recycleView;
    private String c;
    private List<VideoBean.DataBean.ResBean> list;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.video_fragment_layout, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        recycleView = (RecyclerView) view.findViewById(R.id.recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recycleView.setLayoutManager(linearLayoutManager);
        c = getArguments().getString(RedianFragment.CLASS_KIND_KEY);
        list = new ArrayList<>();
        adapter = new VideoAdapter(getActivity(), list);
        recycleView.setAdapter(adapter);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loadData();
    }

    private void loadData() {
        String url = null;
        url = ApiManager.BASE_URL_SJ;
        url = url + ApiManager.RECOMMON_URL + "&c=" + c;
        L.d("url:---VideoFragment::" + url);
        DownString.loadString(url, new DownString.CallBack() {
            @Override
            public void htmlString(String msg) {

                VideoBean nb = new Gson().fromJson(msg, VideoBean.class);
                List<VideoBean.DataBean.ResBean> res = nb.getData().getRes();
                if (list == null) {
                    list = new ArrayList<>();
                }
                list.clear();
                list.addAll(res);
                for (final VideoBean.DataBean.ResBean a : list) {
                    String url2 = a.getVideoUrl();
//                    L.d(url2);
                    //二层
                    DownString.loadString(url2, new DownString.CallBack() {
                        @Override
                        public void htmlString(String msg) {
                            try {
                                JSONObject jo = new JSONObject(msg);
                                jo = jo.getJSONObject("data");
                                String urlStr = jo.getString("playLink");
                                DownString.loadString(urlStr, new DownString.CallBack() {
                                    @Override
                                    public void htmlString(String msg) {
                                        try {
                                            JSONObject jo = new JSONObject(msg);
                                            jo = jo.getJSONObject("data");
                                            String videoUrl = jo.getString("url");
                                            a.aboutRadioUrl = videoUrl;
//                                            L.d("videoUrl:" + list.get(0).aboutRadioUrl);
                                            handler.sendEmptyMessage(0x002);
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
                handler.sendEmptyMessage(0x002);
            }
        });

    }
}
